package com.example.dao.impl;

import com.example.dao.WorkOrderDao;
import com.example.entity.customerAdmin.WorkOrder;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class WorkOrderDaoImpl implements WorkOrderDao {
    @Override
    public List<WorkOrder> selectAll (int page, int limit) {
        List<WorkOrder> workOrderList=new ArrayList<> ();
        Object[] objects=new Object[2];
        objects[0]=page;
        objects[1]=limit;
        ResultSet select = BaseDao.select ("select * from work_order limit ?,?", objects);
        try {
            while (select.next ()){
                WorkOrder workOrder=new WorkOrder ();
                workOrder.setWoId (select.getInt ("wo_id"));
                workOrder.setWoTitle (select.getString ("wo_title"));
                workOrder.setLaunchUser (select.getString ("launch_user"));
                workOrder.setDealUser (select.getString ("deal_user"));
                workOrder.setLaunchTime (select.getString ("launch_time"));
                workOrder.setStates (select.getString ("states"));
                workOrder.setCommentRecord (select.getString ("comment_record"));
                workOrderList.add (workOrder);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return workOrderList;
    }

    @Override
    public int getCount () {
        int i=-1;

        ResultSet select = BaseDao.select ("select count(1) getCount from work_order ", null);
        try {
            while (select.next ()){
                i=select.getInt ("getCount");
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int delete (int id) {
        int i=-1;
        Object[] objects=new Object[1];
        objects[0]=id;
        try {
            i=BaseDao.modify ("delete from work_order where id=?",objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public WorkOrder selectId (int id) {
        WorkOrder workOrder=new WorkOrder ();
        Object[] objects=new Object[1];
        objects[0]=id;
        ResultSet select = BaseDao.select ("select * from work_order where id=?", objects);
        try {
            while (select.next ()){
                workOrder.setWoId (select.getInt ("wo_id"));
                workOrder.setWoTitle (select.getString ("wo_title"));
                workOrder.setLaunchUser (select.getString ("launch_user"));
                workOrder.setDealUser (select.getString ("deal_user"));
                workOrder.setLaunchTime (select.getString ("launch_time"));
                workOrder.setStates (select.getString ("states"));
                workOrder.setCommentRecord (select.getString ("comment_record"));
                return workOrder;
            }
        }catch (Exception e){
            e.printStackTrace ();
        }

        return null;
    }
}
